package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.domainVO.PageInfoVO;
import com.util.HotUtil;
import com.verify.UserVerify;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.domain.Label;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.mapper.living.UserDao;
import com.service.ILabelService;
import com.verify.WeiXinVerify;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import living.com.result.ApiResult;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 处理标签请求
 * </p>
 *
 * @author 大忽悠
 * @create 2022/2/9 11:21
 */
@Api(tags = "标签请求处理")
@RestController
@RequiredArgsConstructor
public class LabelController {
    private final ILabelService iLabelService;

   //@WeiXinVerify(paramIndex = 0,paramName = "Label",weiXinVerifyType = 4)
    //标签不进行审核,涉及openId为null,导致微信审核失败的情况
    @UserVerify
    @ApiOperation("增加一个标签")
    @PostMapping("/labels")
    @ApiOperationSupport(includeParameters = {"label.content"})
    public ApiResult<Integer> addOneLabel(@Validated @RequestBody Label label) {
        iLabelService.save(label);
        return ApiResult.success(label.getId());
    }

    @ApiOperation("获取所有标签")
    @GetMapping("/labels")
    public ApiResult<Map<String,Object>> getAllLabels(@ApiParam("起始页")@RequestParam Integer startPage,
                                               @ApiParam("一页多少条数据")@RequestParam Integer pageSize)
    {
        Page<Label> page=new Page<>(startPage,pageSize);
        return ApiResult.success(PageInfoVO.builder().autoWired(page,iLabelService.page(page,new QueryWrapper<Label>()
                .orderByDesc("hot")).getRecords()));
    }


    @ApiOperation("获取指定标签下的所有问题")
    @GetMapping("/issuesUnderTargetLabel")
    public ApiResult<Map<String,Object>> getIssuesUnderTargetLabel(@ApiParam("标签id")@RequestParam Integer label_id,
                                                                              @ApiParam("起始页")@RequestParam Integer startPage,
                                                                              @ApiParam("一页多少条数据")@RequestParam Integer pageSize)
    {
        return ApiResult.success(iLabelService.getIssuesUnderTargetLabel(label_id,startPage,pageSize));
    }

    @ApiOperation("模糊搜索标签")
    @GetMapping("/vagueSearch")
    public ApiResult<Map<String,Object>> vagueSearch(@ApiParam("标签内容")@RequestParam String content,
                                                     @ApiParam("起始页")@RequestParam Integer startPage,
                                                     @ApiParam("一页多少条数据")@RequestParam Integer pageSize)
    {
        return ApiResult.success(iLabelService.vagueSearch(content,startPage,pageSize));
    }

    @UserVerify
    @ApiOperation("删除指定标签")
    @DeleteMapping("/label")
    public ApiResult<Boolean> delTargetLabel(@ApiParam("标签id")@RequestParam Integer label_id)
    {
        return ApiResult.success(iLabelService.delTargetLabel(label_id));
    }


}
